import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由懒加载
//当打包构建应用时，JavaScript 包会变得非常大，影响页面加载。如果我们能把不同路由对应的组件分割成不同的代码块，然后当路由被访问的时候才加载对应组件，这样就会更加高效。
// import Login from '../views/Login.vue'
// import Home from '../views/Home.vue'
// import Welcome from '../views/Welcome.vue'
const Login = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '../views/Login.vue')
const Home = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '../views/Home.vue')
const Welcome = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '../views/Welcome.vue')
// import Users from '../views/Users.vue'
// import Roles from '../views/Roles.vue'
// import Rights from '../views/Rights.vue'
const Users = () =>
  import(/* webpackChunkName: "user_roles_rights" */ '../views/Users.vue')
const Roles = () =>
  import(/* webpackChunkName: "user_roles_rights" */ '../views/Roles.vue')
const Rights = () =>
  import(/* webpackChunkName: "user_roles_rights" */ '../views/Rights.vue')
// import Goods from '../views/Goods.vue'
// import Params from '../views/Params.vue'
// import Categories from '../views/Categories'
const Goods = () =>
  import(/* webpackChunkName: "goods_params_categories" */ '../views/Goods.vue')
const Params = () =>
  import(/* webpackChunkName: "goods_params_categories" */ '../views/Params.vue')
const Categories = () =>
  import(/* webpackChunkName: "goods_params_categories" */ '../views/Categories')
// import Orders from '../views/Orders.vue'
// import Reports from '../views/Reports.vue'
const Orders = () =>
  import(/* webpackChunkName: "orders_reports" */ '../views/Orders.vue')
const Reports = () =>
  import(/* webpackChunkName: "orders_reports" */ '../views/Reports.vue')
// import Add from '../views/Add.vue'
const Add = () =>
  import(/* webpackChunkName: "add" */ '../views/Add.vue')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/goods',
        component: Goods
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/categories',
        component: Categories
      },
      {
        path: '/orders',
        component: Orders
      },
      {
        path: '/reports',
        component: Reports
      },
      {
        path: '/goods/add',
        component: Add
      },
    ]
  },

]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫   此方法检测localStorage无效，因为localStorage将长时间保存在localStorage中
// 保证是在登录状态下有token才能进入/home页面
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 从哪个路径跳转过来
  // next() 放行   next('/login') 强制跳转
  if (to.path == '/login') { return next() }
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) { return next('/login') }
  next()
})
export default router
